package com.ocom.webapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.dto.*;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.SaffPageOnShowVo;
import com.ocom.common.vo.StaffPageVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.service.HumanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
@RestController
public class HumanController {

    @Autowired
    private HumanService humanService;

    @Autowired
    private HumanClientService humanClientService;

    /**
     * 部门列表（DepartPage/onShow）
     * */
    @Deprecated
    @GetMapping(HumanServiceUrl.URL_DepartPageOnShow)
    public Result DepartPageOnShow(Long comId,Long deptId,String deptName){
        return humanService.DepartPageOnShow(deptId,deptName);
    }

    /**
     * 部门列表（DepartPage/onShow）
     * */
    @PostMapping(HumanServiceUrl.URL_Department_GetDeptList)
    public Result DepartPageList(@RequestBody JSONObject jsonObject){
        Integer show= 0;
        Long deptId=jsonObject.getLong("deptId");
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        if(23 == yoCiUser.getRole()){
            deptId = Long.valueOf(yoCiUser.getDeptId());
        }
        if(deptId==null  ||  deptId == 0){
            show = 1;
        }
        return humanClientService.getDeptList(yoCiUser.getComId(),deptId,show);
    }

    /**
     * 删除部门（DepartPage/delete）
     * */
    @PostMapping(HumanServiceUrl.URL_DepartPageDelete)
    public Result DepartPageDelete(@Validated @RequestBody DeleteDeptDto deleteDeptDto){
        return humanService.DepartPageDelete(deleteDeptDto);
    }



    /**
     * 修改部门
     * */
    @PostMapping(HumanServiceUrl.URL_DepartPageModify)
    public Result DepartPageModify(@Validated @RequestBody ModifyDeptDto modifyDeptDto){
        return humanService.DepartPageModify(modifyDeptDto);
    }


    /**
     * 新增部门
     * */
    @PostMapping(HumanServiceUrl.URL_DepartPageAdd)
    public Result DepartPageAddDept(@Validated @RequestBody AddDeptDto addDeptDto){
        return humanService.DepartPageAddDept(addDeptDto);
    }

    /**
     * 人员列表（StaffPage/onShow）
     * */
    @PostMapping(HumanServiceUrl.URL_StaffPageOnShow)
    public Result StaffPageOnShow(@Validated @RequestBody SaffPageOnShowVo saffPageOnShowVo){
        return humanService.StaffPageOnShow(saffPageOnShowVo);
    }

    /**
     * 人员信息（StaffPage/info）
     * */
    @GetMapping(HumanServiceUrl.URL_StaffPageInfo)
    public Result StaffPageInfo(Long comId,Long pId){
        return humanService.StaffPageInfo(comId,pId);
    }

    /**
     * 新增人员信息（StaffPage/add）
     * */
    @PostMapping(HumanServiceUrl.URL_StaffPageAdd)
    public Result StaffPageAdd(@Validated @RequestBody StaffPageVo staffPageVo){
        if(true)  return ResultUtil.error(ResultEnum.Err_502);
        YoCiUser yoCiUser= SecurityUtils.getYoCiUser();
        staffPageVo.setComId(yoCiUser.getComId());
        return humanService.StaffPageAdd(staffPageVo);
    }
    /**
     * 修改人员信息（StaffPage/modify）
     * */
    @PostMapping(HumanServiceUrl.URL_StaffPageModify)
    public Result StaffPageModify(@Validated @RequestBody StaffPageVo staffPageVo){
        if(true)  return ResultUtil.error(ResultEnum.Err_502);
        YoCiUser yoCiUser= SecurityUtils.getYoCiUser();
        staffPageVo.setComId(yoCiUser.getComId());
        return humanService.StaffPageModify(staffPageVo);
    }


    /**
     * 2.4.5管理员导出列设置（getMngUserForm）
     * */
    @PostMapping(HumanServiceUrl.URL_GetMngUserForm)
    public Result getMngUserFormSelected(@RequestBody GetMngUserFormDto dto){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        dto.setUserId(yoCiUser.getId());
        return humanClientService.getMngUserFormSelected(dto);
    }

    /**
     * 2.4.5管理员导出列设置（getMngUserFormSelected）
     * */
    @PostMapping(HumanServiceUrl.URL_EditMngUserForm)
    public Result editMngUserFormSelected(@RequestBody EditMngUserFormDto dto){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        dto.setUserId(yoCiUser.getId());
        return humanClientService.editMngUserFormSelected(dto);
    }

}
